using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Xml;
using System.Threading;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Sinacor.Infra.UI.Common;
using PSE.Framework.UI.Resources;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public partial class SinacorSplashScreen : System.Windows.Window
	{
		DispatcherTimer timer;
		Grid SplashScreenSymbolPlusLogo;
		Grid SplashScreenSystemNamePlus;
		Grid SplashScreenSystemName;

		protected static readonly DependencyPropertyKey InstalledModulesPropertyKey = DependencyProperty.RegisterReadOnly("InstalledModules", typeof(ObservableCollection<ModuleInformation>), typeof(SinacorSplashScreen), new FrameworkPropertyMetadata());
		public static readonly DependencyProperty ModulePlusProperty = DependencyProperty.Register("ModulePlus", typeof(bool), typeof(SinacorSplashScreen), new FrameworkPropertyMetadata(false));
		public static readonly DependencyProperty VersionProperty = DependencyProperty.Register("Version", typeof(string), typeof(SinacorSplashScreen), new FrameworkPropertyMetadata());
		public static readonly DependencyProperty InstalledModulesProperty = InstalledModulesPropertyKey.DependencyProperty;

		public bool ModulePlus
		{
			get { return (bool)GetValue(ModulePlusProperty); }
			set { SetValue(ModulePlusProperty, value); }
		}

		public SinacorSplashScreen(Version version, IList<ModuleInformation> installedModules)
		{
			this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
			this.InstalledModules = new ObservableCollection<ModuleInformation>();

			IResourceProvider resourceProvider = ResourceProviderManager.GetResourceProvider();

			this.Version = version.ToString();

			foreach (ModuleInformation module in installedModules)
			{
				this.InstalledModules.Add(new ModuleInformation(((string)resourceProvider.TryGetResource(string.Format("Common.Application.Label.{0}", module.Name))), module.Version));
				if (module.Name == "KNL")
					this.ModulePlus = true;
			}

			this.Loaded += new RoutedEventHandler(SplashScreen_Loaded);
			this.SetResourceReference(StyleProperty, "ControlSplashScreen");
		}

		public void Show(int displayTime)
		{
			timer = new DispatcherTimer();
			timer.Interval = new TimeSpan(0, 0, displayTime);
			timer.Tick += new EventHandler(timer_Tick);
			timer.Start();
			base.ShowDialog();
		}

		public string Version
		{
			get { return (string)GetValue(VersionProperty); }
			set { SetValue(VersionProperty, value); }
		}

		public ObservableCollection<ModuleInformation> InstalledModules
		{
			get { return (ObservableCollection<ModuleInformation>)GetValue(InstalledModulesProperty); }
			protected set { SetValue(InstalledModulesPropertyKey, value); }
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			SplashScreenSymbolPlusLogo = this.GetTemplateChild("PART_SplashScreenSymbolPlusLogo") as Grid;
			SplashScreenSystemNamePlus = this.GetTemplateChild("PART_SplashScreenSystemNamePlus") as Grid;
			SplashScreenSystemName = this.GetTemplateChild("PART_SplashScreenSystemName") as Grid;
		}

		void SplashScreen_Loaded(object sender, RoutedEventArgs e)
		{
			verifyModulePlus();
		}

		void timer_Tick(object sender, EventArgs e)
		{
			timer.Stop();
			timer = null;
			this.Close();
		}

		void verifyModulePlus()
		{
			if (ModulePlus)
			{
				SplashScreenSymbolPlusLogo.Visibility = Visibility.Visible;
				SplashScreenSystemNamePlus.Visibility = Visibility.Visible;
				SplashScreenSystemName.Visibility = Visibility.Collapsed;
			}
		}
    }
}